home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cpptutor.arc / CHAP06.TXT < prev    next >
Text File  |  1991-04-28  |  34KB  |  707 lines

  1.  
  2.  
  3.  
  4.                                                         Chapter 6
  5.                                                MORE ENCAPSULATION
  6.  
  7. The purpose of this chapter is to illustrate how to use some of the
  8. traditional aspects of C or C++ with classes and objects.  Pointers
  9. to an object as well as pointers within an object will be
  10. illustrated.  Arrays embedded within an object, and an array of
  11. objects will be illustrated.  Since objects are simply another C
  12. data construct, all of these things are possible and can be used
  13. if needed.
  14.  
  15. In order to have a systematic study, we will use the program named
  16. BOXES1.CPP from the last chapter as a starting point and we will
  17. add a few new constructs to it for each example program.  You will
  18. recall that it was a very simple program with the class definition,
  19. the class implementation, and the main calling program all in one
  20. file.  This was selected as a starting point because we will
  21. eventually make changes to all parts of the program and it will be
  22. convenient to have it all in a single file for illustrative
  23. purposes.  It must be kept in mind however that the proper way to
  24. use these constructs is to separate them into the three files as
  25. was illustrated in BOX.HPP, BOX.CPP, and BOXES2.CPP in the last
  26. chapter.  This allows the implementor of box to supply the user
  27. with only the interface, namely  BOX.HPP.  Not giving him the
  28. implementation file named BOX.CPP, is practicing the technique of
  29. information hiding.
  30.  
  31. As we have said many times, it seems silly to break up such a small
  32. program into three separate files, and it is sort of silly.  The
  33. last chapter of this tutorial will illustrate a program large
  34. enough to require dividing the program up into separate files.
  35.  
  36.  
  37. AN ARRAY OF OBJECTS
  38. _________________________________________________________________
  39.  
  40. Examine the file named OBJARRAY.CPP for our      ================
  41. first example of an array of objects.  This file   OBJARRAY.CPP
  42. is identical to the file named BOX1.CPP until we ================
  43. come to line 41 where an array of 4 boxes are
  44. declared.
  45.  
  46. Recalling the operation of the constructor you will remember that
  47. each of the four box objects will be initialized to the values
  48. defined within the constructor since each box will go through the
  49. constructor as they are declared.  In order to declare an array of
  50. objects, a constructor for that object must not require any
  51. parameters.  (We have not yet illustrated a constructor with
  52. initializing parameters, but we will in the next program.)  This
  53. is an efficiency consideration since it would probably be an error
  54. to initialize all elements of an array of objects to the same
  55. value.  We will see the results of executing the constructor when
  56. we compile and execute the file later.
  57.  
  58.                                                          Page 6-1
  59.  
  60.                                    Chapter 6 - More Encapsulation
  61.  
  62. Line 46 defines a for loop that begins with 1 instead of the normal
  63. starting index for an array leaving the first object, named
  64. group[0], to use the default values stored when the constructor was
  65. called.  You will observe that sending a message to one of the
  66. objects uses the same construct as is used for any object.  The
  67. name of the array followed by its index in square brackets is used
  68. to send a message to one of the objects in the array.  This is
  69. illustrated in line 47 and the operation of that code should be
  70. clear to you.  The other method is called in the output statement
  71. in lines 54 and 55 where the area of the four boxes in the group
  72. array are listed on the monitor.
  73.  
  74. Another fine point should be pointed out.  The integer variable
  75. named index is declared in line 46 and is still available for use
  76. in line 53 since we have not yet left the enclosing block which
  77. begins in line 40 and extends to line 62.
  78.  
  79. An extra variable was included for illustration, the one named
  80. extra_data in line seven.  Since the keyword static is used to
  81. modify this variable in line 7, it is an external variable and only
  82. one copy of this variable will ever exist.  All seven objects of
  83. this class share a single copy of this variable which is global to
  84. the objects defined in line 41.  Line 20 of the constructor sets
  85. the single global variable to 1 each time an object is declared. 
  86. Only one assignment is necessary so the other six are actually
  87. wasted code.  To illustrate that there is only one variable shared
  88. by all objects of this class, the method to read its value also
  89. increments it.  Each time it is read in lines 57 through 61, it is
  90. incremented and the result of the execution proves that there is
  91. only a single variable shared by all objects of this class.  You
  92. will also note that this method is declared within the class
  93. declaration so it will be assembled into the final program as
  94. inline code.
  95.  
  96. Be sure you understand this program and especially the static
  97. variable, then compile and execute it to see if you get the same
  98. result as listed at the end of the program.
  99.  
  100.  
  101. A STRING WITHIN AN OBJECT
  102. _________________________________________________________________
  103.  
  104. Examine the program named OBJSTRNG.CPP for our   ================
  105. first example of an object with an embedded        OBJSTRNG.CPP
  106. string.  Actually, the object does not have an   ================
  107. embedded string, it has an embedded pointer, but
  108. the two work so closely together that we can
  109. study one and understand both.
  110.  
  111. You will notice that line 7 contains a pointer to char named
  112. line_of_text.  The constructor contains an input parameter which
  113. is a pointer to a string and will be assigned to the stored pointer
  114. named line_of_text within the constructor.  We could have defined
  115.  
  116.                                                          Page 6-2
  117.  
  118.                                    Chapter 6 - More Encapsulation
  119.  
  120. the variable line_of_text as an actual array in the class, then
  121. used strcpy() to copy the string into the object and everything
  122. would have worked the same, but we will leave that as an exercise
  123. for you at the end of this chapter.  It should be clear to you that
  124. if a single parameter can be passed to a constructor, any number
  125. can be passed.
  126.  
  127. You will notice that when the three boxes are declared this time,
  128. we supply a string constant as an actual parameter with each
  129. declaration which is used by the constructor to assign the string
  130. pointer some data to point to.  When we call get_area() in lines
  131. 48 through 53, we get the message displayed and the area returned. 
  132. It would be prudent to put these operations in separate methods
  133. since there is no apparent connection between printing the message
  134. and calculating the area, but it was written this way to illustrate
  135. that it can be done.  What this really says is that it is possible
  136. to have a method that has a side effect, the message output to the
  137. monitor, and a return value, the area of the box.  However, as we
  138. discussed in chapter 4 when we studied DEFAULT.CPP, the order of
  139. evaluation is sort of funny, so we broke each line into two lines.
  140.  
  141. After you understand this program, compile and execute it.
  142.  
  143.  
  144. AN OBJECT WITH AN INTERNAL POINTER
  145. _________________________________________________________________
  146.  
  147. The program named OBJINTPT.CPP is our first      ================
  148. example program with an embedded pointer which     OBJINTPT.CPP
  149. will be used for dynamic allocation of data.     ================
  150.  
  151. In line 7 we declare a pointer to an integer
  152. variable, but it is only a pointer, there is no storage associated
  153. with it.  The constructor therefore allocates an integer type
  154. variable on the heap for use with this pointer in line 21.  It
  155. should be clear to you that the three objects created in line 45
  156. each contain a pointer which points into the heap to three
  157. different locations.  Each object has its own dynamically allocated
  158. variable for its own private use.  Moreover each has a value of 112
  159. stored in its dynamically allocated data because line 22 stores
  160. that value in each of the three locations, once for each call to
  161. the constructor.
  162.  
  163. In such a small program, there is no chance that we will exhaust
  164. the heap, so no test is made for unavailable memory.  In a real
  165. production program, it would be expedient to test that the value
  166. of the returned pointer is not NULL to assure that the data
  167. actually did get allocated.
  168.  
  169. The method named set() has three parameters associated with it and
  170. the third parameter is used to set the value of the new dynamically
  171. allocated variable.  There are two messages passed, one to the
  172. small box and one to the large box.  As before, the medium box is
  173. left with its default values.
  174.  
  175.                                                          Page 6-3
  176.  
  177.                                    Chapter 6 - More Encapsulation
  178.  
  179.  
  180. The three areas are displayed followed by the three stored values
  181. in the dynamically allocated variables, and we finally have a
  182. program that requires a destructor in order to be completely
  183. proper.  If we simply leave the scope of the objects as we do when
  184. we leave the main program, we will leave the three dynamically
  185. allocated variables on the heap with nothing pointing to them. 
  186. They will be inaccessible and will therefore represent wasted
  187. storage on the heap.  For that reason, the destructor is used to
  188. delete the three variables as their respective objects go out of
  189. existence.  In this case, lines 37 and 38 assign values to
  190. variables that will be automatically deleted.  Even though these
  191. lines of code really do no good, they are legal statements.
  192.  
  193. Actually, in this particular case, the three variables will be
  194. automatically reclaimed when we return to the operating system
  195. because all program cleanup is done for us at that time.  If this
  196. were a function that was called by another function however, the
  197. heap space would be wasted.  This is an illustration of good
  198. programming practice, that of cleaning up after yourself when you
  199. no longer need some dynamically allocated variables.
  200.  
  201. One other construct should be mentioned once again, that of the
  202. inline method implementations in line 11 and 12.  As we mentioned
  203. in chapter 5 and repeated earlier in this chapter, inline functions
  204. can be used where speed is of the utmost in importance since the
  205. code is assembled inline rather than by actually making a method
  206. call.  Since the code is defined as part of the declaration, the
  207. system will assemble it inline, and a separate implementation for
  208. these methods is not needed.  Remember that if the inline code is
  209. too involved, the compiler is allowed to ignore the inline request
  210. and actually assemble it as a separate method, but it will do it
  211. invisibly to you and will probably not even tell you about it.
  212.  
  213. Remember that we are interested in using information hiding and
  214. inline code prevents hiding of the implementation, putting it out
  215. in full view.  Many times you will be more interested in speeding
  216. up a program than you are in hiding a trivial implementation. 
  217. Since most inline methods are trivial, feel free to use the inline
  218. code construct.
  219.  
  220. Be sure to compile and execute this program.
  221.  
  222.  
  223.  
  224. A DYNAMICALLY ALLOCATED OBJECT
  225. _________________________________________________________________
  226.  
  227. Examine the file named OBJDYNAM.CPP for our      ================
  228. first look at a dynamically allocated object.      OBJDYNAM.CPP
  229. This is not any different than any other         ================
  230. dynamically allocated object, but an example is
  231. always helpful.
  232.  
  233.  
  234.                                                          Page 6-4
  235.  
  236.                                    Chapter 6 - More Encapsulation
  237.  
  238. In line 39 we declare a pointer to an object of type box and since
  239. it is only a pointer with nothing to point to, we dynamically
  240. allocate an object for it in line 44, with the object being created
  241. on the heap just like any other dynamically allocated variable. 
  242. When the object is created in line 44, the constructor is called
  243. automatically to assign values to the two internal storage
  244. variables.  Note that the constructor is not called when the
  245. pointer is declared since there is nothing to initialize.  It is
  246. called when the object is allocated.
  247.  
  248. Reference to the components of the object are handled in much the
  249. same way that structure references are made, through use of the
  250. pointer operator as illustrated in lines 50 through 52.  Of course
  251. you can use the pointer dereferencing method without the arrow such
  252. as (*point).set(12, 12); as a replacement for line 51 but the arrow
  253. notation is much more universal and should be used.  Finally, the
  254. object is deleted in line 54 and the program terminates.  If there
  255. were a destructor for this class, it would be called as part of the
  256. delete statement to clean up the object prior to deletion.
  257.  
  258. You have probably noticed by this time that the use of objects is
  259. not much different from the use of structures.  Be sure to compile
  260. and execute this program after you have studied it thoroughly.
  261.  
  262.  
  263. AN OBJECT WITH A POINTER TO ANOTHER OBJECT
  264. _________________________________________________________________
  265.  
  266. The program named OBJLIST.CPP contains an object  ===============
  267. with an internal reference to another object of     OBJLIST.CPP
  268. its own class.  This is the standard structure    ===============
  269. used for a singly linked list and we will keep
  270. the use of it very simple in this program.
  271.  
  272. The constructor contains the statement in line 21 which assigns the
  273. pointer the value of NULL to initialize the pointer.  This is a
  274. good idea for all of your programming, don't allow any pointer to
  275. point off into space, but initialize all pointers to something. 
  276. By assigning the pointer within the constructor, you guarantee that
  277. every object of this class will automatically have its pointer
  278. initialized.  It will be impossible to overlook the assignment of
  279. one of these pointers.
  280.  
  281. Two additional methods are declared in lines 12 and 13 with the one
  282. in line 13 having a construct we have not yet mentioned in this
  283. tutorial.  This method returns a pointer to an object of the box
  284. class.  As you are aware, you can return a pointer to a struct in
  285. standard C, and this is a parallel construct in C++.  The
  286. implementation in lines 48 through 51 returns the pointer stored
  287. within the object.  We will see how this is used when we get to the
  288. actual program.
  289.  
  290. An extra pointer named box_pointer is declared in the main program
  291. for use later and in line 66 we make the embedded pointer within
  292.  
  293.                                                          Page 6-5
  294.  
  295.                                    Chapter 6 - More Encapsulation
  296.  
  297. the small box point to the medium box, and in line 67 we make the
  298. embedded pointer within the medium box point to the large box.  We
  299. have effectively generated a linked list with three elements.  In
  300. line 69 we make the extra pointer point to the small box. 
  301. Continuing in line 70 we use it to refer to the small box and
  302. update it to the value contained in the small box which is the
  303. address of the medium box.  We have therefore traversed from one
  304. element of the list to another by sending a message to one of the
  305. objects.  If line 70 were repeated exactly as shown, it would cause
  306. the extra pointer to refer to the large box, and we would have
  307. traversed the entire linked list which is only composed of three
  308. elements.
  309.  
  310.  
  311. ANOTHER NEW KEYWORD this
  312. _________________________________________________________________
  313.  
  314. Another new keyword is available in C++, the keyword this.  The
  315. word this is defined within any object as being a pointer to the
  316. object in which it is contained.  It is implicitly declared as;
  317.  
  318.    class_name *this;
  319.  
  320. and is initialized to point to the object for which the member
  321. function is invoked.  This pointer is most useful when working with
  322. pointers and especially with a linked list when you need to
  323. reference a pointer to the object you are inserting into the list. 
  324. The keyword this is available for this purpose and can be used in
  325. any object.  Actually the proper way to refer to any variable
  326. within a list is through use of the predefined pointer this, by
  327. writing this->variable_name, but the compiler assumes the pointer,
  328. and we can simplify every reference by omitting the pointer.  Use
  329. of the keyword this is not illustrated in a program at this point,
  330. but will be used in one of the larger example programs later in
  331. this tutorial.
  332.  
  333. You should study this program until you understand it completely
  334. then compile and execute it in preparation for our next example
  335. program.
  336.  
  337.  
  338. A LINKED LIST OF OBJECTS
  339. _________________________________________________________________
  340.  
  341. The next example program in this chapter is       ===============
  342. named OBJLINK.CPP and is a complete example of      OBJLINK.CPP
  343. a linked list written in object oriented          ===============
  344. notation.
  345.  
  346. This program is very similar to the last one.  In fact it is
  347. identical until we get to the main program.  You will recall that
  348. in the last program the only way we had to set or use the embedded
  349. pointer was through use of the two methods named point_at_next()
  350. and get_next() which are listed in lines 40 through 51 of the
  351.  
  352.                                                          Page 6-6
  353.  
  354.                                    Chapter 6 - More Encapsulation
  355.  
  356. present program.  We will use these to build up our linked list
  357. then traverse and print the list.  Finally, we will delete the
  358. entire list to free the space on the heap.
  359.  
  360. In lines 56 to 58 we declare three pointers for use in the program. 
  361. The pointer named start will always point to the beginning of the
  362. list, but temp will move down through the list as we create it. 
  363. The pointer named box_pointer will be used for the creation of each
  364. box.  We execute the loop in lines 61 through 69 to generate the
  365. list where line 62 dynamically allocates a new object of the box
  366. class and line 63 fills it with nonsense data for illustration. 
  367. If this is the first element in the list, the start pointer is set
  368. to point to this element, but if elements already exist, the last
  369. element in the list is assigned to point to the new element.  In
  370. either case, the temp pointer is assigned to point to the last
  371. element of the list, in preparation for adding another element if
  372. there is another element to be added.
  373.  
  374. In line 72, the pointer named temp is pointed to the first element
  375. and it is used to increment its way through the list by updating
  376. itself in line 75 during each pass through the loop.  When temp has
  377. the value of NULL, which it gets from the last element of the list,
  378. we are finished traversing the list.
  379.  
  380. Finally, we delete the entire list by starting at the beginning and
  381. deleting one element each time we pass through the loop in lines
  382. 79 through 84.
  383.  
  384. A careful study of the program will reveal that it does indeed
  385. generate a linked list of ten elements, each element being an
  386. object of class box.  The length of this list is limited by the
  387. practicality of how large a list we desire to print out, but it
  388. could be lengthened to many thousands of these simple elements
  389. provided you have enough memory available to store them all.
  390.  
  391. Once again, the success of the dynamic allocation is not checked
  392. as it should be in a correctly written program.  Be sure to compile
  393. and execute this example program.
  394.  
  395.  
  396.  
  397. NESTING OBJECTS
  398. _________________________________________________________________
  399.  
  400. Examine the program named NESTING.CPP for an      ===============
  401. example of nesting classes which results in         NESTING.CPP
  402. nested objects.  A nested object could be         ===============
  403. illustrated with your computer in a rather
  404. simple manner.  The computer itself is composed
  405. of many items which work together but work entirely differently,
  406. such as a keyboard, a disk drive, and a power supply.  The computer
  407. is composed of these very dissimilar items and it is desireable to
  408. discuss the keyboard separately from the disk drive because they
  409. are so different.  A computer class could be composed of several
  410.  
  411.                                                          Page 6-7
  412.  
  413.                                    Chapter 6 - More Encapsulation
  414.  
  415. objects that are dissimilar by nesting the dissimilar classes
  416. within the computer class.
  417.  
  418. If however, we wished to discuss disk drives, we may wish to
  419. examine the characteristics of disk drives in general, then examine
  420. the details of a hard disk, then the differences of floppy disks,
  421. and finally bernolli drives.  This would involve inheritance
  422. because much of the data about all three drives could be
  423. characterized and applied to the generic disk drive then used to
  424. aid in the discussion of the other three.  We will study
  425. inheritance in the next three chapters, but for now we will look
  426. at the embedded or nested class.
  427.  
  428. This example program contains a class named box which contains an
  429. object of another class embedded within it in line 16, the
  430. mail_info class.  This object is available for use only within the
  431. class implementation of box because that is where it is defined. 
  432. The main program has objects of class box defined but no objects
  433. of class mail_info, so the mail_info class cannot be referred to
  434. in the main program.  In this case, the mail_info class object is
  435. meant to be used internally to the box class and one example is
  436. given in line 21 where a message is sent to the label.set() method
  437. to initialize the variables.  Additional methods could be used as
  438. needed, but these are given as an illustration of how they can be
  439. called.
  440.  
  441. Of prime importance is the fact that there are never any objects
  442. of the mail_info class declared directly in the main program, they
  443. are inherently declared when the enclosing objects of class box are
  444. declared.  Of course objects of the mail_info class could be
  445. declared and used in the main program if needed, but they are not
  446. in this example program.  In order to be complete, the box class
  447. should have one or more methods to use the information stored in
  448. the object of the mail_info class.  Study this program until you
  449. understand the new construct, then compile and execute it.
  450.  
  451. If the class and the nested classes require parameter lists for
  452. their respective constructors an initialization list can be given. 
  453. This is a very advanced technique, and it is rarely needed, so it
  454. is beyond the scope of this tutorial.  See Bjarne Stroustrup's book
  455. for further details if you need more information on the use of this
  456. technique.
  457.  
  458.  
  459. OPERATOR OVERLOADING
  460. _________________________________________________________________
  461.  
  462. The example file named OPOVERLD.CPP contains     ================
  463. examples of overloading operators.  This allows    OPOVERLD.CPP
  464. you to define a class of objects and redefine    ================
  465. the use of the normal operators.  The end result
  466. is that objects of the new class can be used in
  467. as natural a manner as the predefined types.  In fact, they seem
  468. to be a part of the language rather than your own add-on.
  469.  
  470.                                                          Page 6-8
  471.  
  472.                                    Chapter 6 - More Encapsulation
  473.  
  474.  
  475. In this case we overload the + operator and the * operator, with
  476. the declarations in lines 10 through 12, and the definitions in
  477. lines 16 through 40.  The methods are declared as friend functions
  478. so we can use the double parameter functions as listed.  If we did
  479. not use the friend construct, the function would be a part of one
  480. of the objects and that object would be the object to which the
  481. message was sent.  Including the friend construct allows us to
  482. separate this method from the object and call the method with infix
  483. notation.  Using this technique, it can be written as object1 +
  484. object2 rather than object1.operator+(object2).  Also, without the
  485. friend construct we could not use an overloading with an int type
  486. variable for the first parameter because we can not send a message
  487. to an integer type variable such as int.operator+(object).  Two of
  488. the three operator overloadings use an int for the first parameter
  489. so it is necessary to declare them as friend functions.
  490.  
  491. The header in line 16 illustrates the first overloading where the
  492. + operator is overloaded by giving the return type followed by the
  493. keyword operator and the operator we wish to overload.  The two
  494. formal parameters and their types are then listed in the
  495. parentheses and the normal function operations are given in the
  496. implementation of the function in lines 18 through 21.  There is
  497. nothing unusual about this implementation, it should be easily
  498. understood by you at this point.  For purposes of illustration,
  499. some silly mathematics are performed in the method implementation,
  500. but any desired operations can be done.
  501.  
  502. The biggest difference occurs in line 56 where this method is
  503. called by using the infix notation instead of the usual message
  504. sending format.  Since the variables small and medium are objects
  505. of the box class, the system will search for a way to use the +
  506. operator on two objects of class box and will find it in the
  507. overloaded operator+ method we have just discussed.  The operations
  508. within the method implementation can be anything we need them to
  509. be, and they are usually much more meaningful than the silly math
  510. included here.
  511.  
  512. In line 58 we ask the system to add an int type constant to an
  513. object of class box, so the system finds the other overloading of
  514. the + operator beginning in line 25 to perform this operation. 
  515. Also in line 60 we ask the system to use the * operator to do
  516. something to an int constant and an object of class box, which it
  517. satisfies by finding the method in lines 34 through 40.  Note that
  518. it would be illegal to attempt to use the * operator the other way
  519. around, namely large * 4 since we did not define a method to use
  520. the two types in that order.  Another overloading could be given
  521. with reversed types, and we could use the reverse order in a
  522. program.
  523.  
  524. You will notice that when using operator overloading, we are also
  525. using function name overloading since some of the function names
  526. are the same.
  527.  
  528.  
  529.                                                          Page 6-9
  530.  
  531.                                    Chapter 6 - More Encapsulation
  532.  
  533. When we use operator overloading in this manner, we actually make
  534. our programs look like the class is a natural part of the language
  535. since it is integrated into the language so well.  C++ is therefore
  536. an extendible language and can be molded to fit the mechanics of
  537. the problem at hand.
  538.  
  539.  
  540. OPERATOR OVERLOADING CAVEATS
  541. _________________________________________________________________
  542.  
  543. Each new topic we study has its pitfalls which must be warned
  544. against and the topic of operator overloading seems to have the
  545. record for pitfalls since it is so prone to misuse and has several
  546. problems.  The overloading of operators is only available for
  547. classes, you cannot redefine the operators for the predefined
  548. simple types.  This would probably be very silly anyway since the
  549. code could be very difficult to read if you changed some of them
  550. around.
  551.  
  552. The logical and (&&) and the logical or (||) operators can be
  553. overloaded for the classes you define, but they will not operate
  554. as short circuit operators.  All members of the logical
  555. construction will be evaluated with no regard concerning the
  556. outcome.  Of course the normal predefined logical operators will
  557. continue to operate as short circuit operators as expected, but not
  558. the overloaded ones.
  559.  
  560. If the increment (++) or decrement (--) operators are overloaded,
  561. the system has no way of telling whether the operators are used as
  562. preincrement or postincrement operators.  Which method is used is
  563. implementation dependent, so you should use them in such a way that
  564. it doesn't matter which is used.
  565.  
  566. Be sure to compile and execute OPOVERLD.CPP before continuing on
  567. to the next example program.
  568.  
  569.  
  570. FUNCTION OVERLOADING IN A CLASS
  571. _________________________________________________________________
  572.  
  573. Examine the program named FUNCOVER.CPP for an    ================
  574. example of function name overloading within a      FUNCOVER.CPP
  575. class.  In this program the constructor is       ================
  576. overloaded as well as one of the methods to
  577. illustrate what can be done.
  578.  
  579. This file illustrates some of the uses of overloaded names and a
  580. few of the rules for their use.  You will recall that the function
  581. selected is based on the number and types of the formal parameters,
  582. and the type of the return value.
  583.  
  584. In this case there are three constructors.  The constructor which
  585. is actually called is selected by the number and types of the
  586. parameters in the definition.  In line 77 of the main program the
  587.  
  588.                                                         Page 6-10
  589.  
  590.                                    Chapter 6 - More Encapsulation
  591.  
  592. three objects are declared, each with a different number of
  593. parameters and inspection of the results will indicate that the
  594. correct constructor was called based on the number of parameters. 
  595. Since a constructor does not have a return value, according to the
  596. definition of the C++ language, it cannot be a part of the
  597. selection criteria of a constructor.
  598.  
  599. In the case of the other overloaded methods, the number and type
  600. of parameters is clearly used to select the proper method.  You
  601. will notice that one method uses a single integer and another uses
  602. a single float type variable, but the system is able to select the
  603. correct one.  As many overloadings as desired can be used provided
  604. that all of the parameter patterns are unique.
  605.  
  606. You may be thinking that this is a silly thing to do but it is, in
  607. fact, a very important topic.  Throughout this tutorial we have
  608. been using an overloaded operator and you haven't been the least
  609. confused over it.  It is the cout operator which operates as an
  610. overloaded function since the way it outputs data is a function of
  611. the type of its input variable or the field we ask it to display. 
  612. Many programming languages have overloaded output functions so you
  613. can output any data with the same function name. 
  614.  
  615. Be sure to compile and execute FUNCOVER.CPP.
  616.  
  617.  
  618. SEPARATE COMPILATION
  619. _________________________________________________________________
  620.  
  621. Separate compilation is available with C++ and it follows the
  622. identical rules as given for ANSI-C separate compilation.  As
  623. expected, separately compiled files can be linked together. 
  624. However, since classes are used to define objects, the nature of
  625. C++ separate compilation is considerably different from that used
  626. for ANSI-C.  This is because the classes used to create the objects
  627. are not considered as external variables, but as included classes. 
  628. This makes the overall program look different from a pure ANSI-C
  629. program.
  630.  
  631. As we have been declaring and using classes in these last two
  632. chapters, we have been including the classes with the #include
  633. preprocessor directive rather than by using the ANSI-C extern
  634. keyword.  Your programs will take on a different appearance as you
  635. gain experience in C++.
  636.  
  637.  
  638. WHAT SHOULD BE THE NEXT STEP?
  639. _________________________________________________________________
  640.  
  641. At this point you have learned enough C++ to write meaningful
  642. programs and it would be to your advantage to stop studying and
  643. begin using the knowledge you have gained.  Because C++ is an
  644. extension to ANSI-C, it can be learned in smaller pieces than would
  645. be required if you are learning a completely new language.  You
  646.  
  647.                                                         Page 6-11
  648.  
  649.                                    Chapter 6 - More Encapsulation
  650.  
  651. have learned enough to study and completely understand the example
  652. program given in chapter 12, the Flyaway adventure game.  You
  653. should begin studying this program now.
  654.  
  655. One of your biggest problems is learning to think in terms of
  656. object oriented programming.  It is not a trivial problem if you
  657. have been programming in procedural languages for any significant
  658. length of time.  However, it can be learned by experience, so you
  659. should begin trying to think in terms of classes and objects
  660. immediately.  Your first project should use only a small number of
  661. objects and the remainder of code can be completed in standard C
  662. using procedural methods.  As you gain experience, you will write
  663. more of the code for any given project using classes and objects
  664. but every project will eventually be completed in procedural code.
  665.  
  666. After you have programmed for a while using the techniques covered
  667. up to this point in the tutorial, you can continue on to the next
  668. few chapters which will discuss inheritance and virtual functions.
  669.  
  670.  
  671. PROGRAMMING EXERCISES
  672. _________________________________________________________________
  673.  
  674. 1.   Rewrite OBJSTRNG.CPP to use an array instead of only a pointer
  675.      to the array and copy the string in the constructor.  Add
  676.      another method to allow you to change the value stored in the
  677.      string by sending a message from the main program and use the
  678.      new method to change the value stored in each of the three
  679.      objects.
  680.  
  681. 2.   Modify OBJDYNAM.CPP to make the objects named small and medium
  682.      pointers, then dynamically allocate them prior to using them.
  683.  
  684. 3.   Modify the loop in line 61 of OBJLINK.CPP so that the loop
  685.      will store 1000 elements in the list before stopping.  You
  686.      will probably wish to remove the printout from line 74 so the
  687.      program will stop in a reasonable time.  You may also get an
  688.      integer overflow indication if you send a message to
  689.      get_area() with such large numbers.  That will depend upon
  690.      your compiler.  You should also add a test to assure that the
  691.      memory did not become exhausted after each dynamic allocation.
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.                                                         Page 6-12
  707.